import es
import playerlib

from wcs.libs.players import players
from wcs.libs.items import items

itemName = 'Lightning Flash'
item = items.addItem(itemName)
item.setGroup("attack")
item.setCost(3000)
item.setDescription("You have a 15% chance of blinding your victims.")
item.setMaxCount(1)
item.aliveToUse = True
        
def player_hurt(event_var):
    """
    Executed when a player is hurt. If the attacker has this item active, then
    do a random dice throw. If the 15% chance is successful, then blind the
    victim for 2 seconds.
    """
    attacker = int(event_var["attacker"]) if event_var["attacker"] else 0
    userid = event_var["userid"]
    if attacker:
        player = players[attacker]
        if player.getItem(itemName).count:
            if random.randint(1, 100) > 15:
                return
            player = playerlib.getPlayer(userid)
            player.flash(200, 2)
            text = ("#greenLightning Flash #default- #lightgreenYou have " + 
               "blinded #green%s #lightgreenfor 2 seconds.")
            es.tell(attacker, "#multi", text % event_var['es_username'])
            text = ("#greenLightning Flash #default- #lightgreenYou have " + 
               "been blinded by #green%s #lightgreenfor 2 seconds.")
            es.tell(userid, "#multi", text % event_var['es_attackername'])